\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\input{../labStyle}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}



\license{This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported (\web{http://creativecommons.org/licenses/by-sa/3.0}). This lab was adapted for OpenIntro by Andrew Bray and Mine \c{C}etinkaya-Rundel from a lab written by Mark Hansen of UCLA Statistics.}

\section*{Lab 1: Introduction to data}

Some define Statistics as the field that focuses on turning information into knowledge.  The first step in that process is to summarize and describe the raw information - the data.  In this lab, you will gain insight into public health by generating simple graphical and numerical summaries of a data set collected by the Centers for Disease Control and Prevention (CDC).  As this is a large data set, along the way you'll also learn the indispensable skills of data processing and subsetting.

\subsection*{Getting started}
The Behavioral Risk Factor Surveillance System (BRFSS) is an annual telephone survey of 350,000 people in the United States. As its name implies, the BRFSS is designed to identify risk factors in the adult population and report emerging health trends. For example, respondents are asked about their diet and weekly physical activity, their HIV/AIDS status, possible tobacco use, and even their level of healthcare coverage. The BRFSS Web site (\web{http://www.cdc.gov/brfss}) contains a complete description of the survey, including the research questions that motivate the study and many interesting results derived from the data.

We will focus on a random sample of 20,000 people from the BRFSS survey conducted in 2000. While there are over 200  variables in this data set, we will work with a small subset.

We begin by loading the data set of 20,000 observations into the R workspace. After launching RStudio, enter the following command.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{source}\hlstd{(}\hlstr{"http://www.openintro.org/stat/data/cdc.R"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

The data set \hlstd{cdc} that shows up in your workspace is a \emph{data matrix}, with each row representing a \emph{case} and each column representing a \emph{variable}.  R calls this data format a \emph{data frame}, which is a term that will be used throughout the labs.

To view the names of the variables, type the command

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{names}\hlstd{(cdc)}
\end{alltt}
\end{kframe}
\end{knitrout}

This returns the names \hlstd{genhlth}, \hlstd{exerany}, \hlstd{hlthplan}, \hlstd{smoke100}, \hlstd{height}, \hlstd{weight}, \hlstd{wtdesire}, \hlstd{age}, and \hlstd{gender}. Each one of these variables corresponds to a question that was asked in the survey.  For example, for \hlstd{genhlth}, respondents were asked to evaluate their general health, responding either excellent, very good, good, fair or poor. The \hlstd{exerany} variable indicates whether the respondent exercised in the past month (1) or did not (0). Likewise, \hlstd{hlthplan} indicates whether the respondent had some form of health coverage (1) or did not (0). The \hlstd{smoke100} variable indicates whether the respondent had smoked at least 100 cigarettes in her lifetime. The other variables record the respondent's \hlstd{height} in inches, \hlstd{weight} in pounds as well as their desired weight, \hlstd{wtdesire}, \hlstd{age} in years, and \hlstd{gender}.

\begin{exercise}
How many cases are there in this data set?  How many variables?  For each variable, identify its data type (e.g. categorical, discrete).
\end{exercise}

We can have a look at the first few entries (rows) of our data with the command

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{head}\hlstd{(cdc)}
\end{alltt}
\end{kframe}
\end{knitrout}

and similarly we can look at the last few by typing

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{tail}\hlstd{(cdc)}
\end{alltt}
\end{kframe}
\end{knitrout}

You could also look at \emph{all} of the data frame at once by typing its name into the console, but that might be unwise here.  We know \hlstd{cdc} has 20,000 rows, so viewing the entire data set would mean flooding your screen.  It's better to take small peeks at the data with \hlkwd{head}, \hlkwd{tail} or the subsetting techniques that you'll learn in a moment.

\subsection*{Summaries and tables}
The BRFSS questionnaire is a massive trove of information.  A good first step in any analysis is to distill all of that information into a few summary statistics and graphics.  As a simple example, the function \hlkwd{summary} returns a numerical summary: minimum, first quartile, median, mean, second quartile, and maximum. For \hlstd{weight} this is

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(cdc}\hlopt{$}\hlstd{weight)}
\end{alltt}
\end{kframe}
\end{knitrout}

R also functions like a very fancy calculator.  If you wanted to compute the interquartile range for the respondents' weight, you would look at the output from the summary command above and then enter

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlnum{190} \hlopt{-} \hlnum{140}
\end{alltt}
\end{kframe}
\end{knitrout}

R also has built-in functions to compute summary statistics one by one.  For instance, to calculate the mean, median, and variance of \hlstd{weight}, type 

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{mean}\hlstd{(cdc}\hlopt{$}\hlstd{weight)}

\hlkwd{var}\hlstd{(cdc}\hlopt{$}\hlstd{weight)}

\hlkwd{median}\hlstd{(cdc}\hlopt{$}\hlstd{weight)}
\end{alltt}
\end{kframe}
\end{knitrout}

While it makes sense to describe a quantitative variable like \hlstd{weight} in terms of these statistics, what about categorical data?  We would instead consider the sample frequency or relative frequency distribution.  The function \hlkwd{table} does this for you by counting the number of times each kind of response was given. For example, to see the number of people who have smoked 100 cigarettes in their lifetime, type

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{table}\hlstd{(cdc}\hlopt{$}\hlstd{smoke100)}
\end{alltt}
\end{kframe}
\end{knitrout}

or instead look at the relative frequency distribution by typing

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{table}\hlstd{(cdc}\hlopt{$}\hlstd{smoke100)}\hlopt{/}\hlnum{20000}
\end{alltt}
\end{kframe}
\end{knitrout}

Notice how R automatically divides all entries in the table by 20,000 in the command above. This is similar to something we observed in the last lab; when we multiplied or divided a vector with a number, R applied that action across entries in the vectors. As we see above, this also works for tables. Next, we make a bar plot of the entries in the table by putting the table inside the barplot command.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{barplot}\hlstd{(}\hlkwd{table}\hlstd{(cdc}\hlopt{$}\hlstd{smoke100))}
\end{alltt}
\end{kframe}
\end{knitrout}

Notice what we've done here! We've computed the table of \hlstd{cdc\$smoke100} and then immediately applied the graphical function, \hlkwd{barplot}. This is an important idea: R commands can be nested. You could also break this into two steps by typing the following:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{smoke} \hlkwb{<-} \hlkwd{table}\hlstd{(cdc}\hlopt{$}\hlstd{smoke100)}

\hlkwd{barplot}\hlstd{(smoke)}
\end{alltt}
\end{kframe}
\end{knitrout}

Here, we've made a new object, a table, called \hlstd{smoke} (the contents of which we can see by typing \hlstd{smoke} into the console) and then used it in as the input for \hlkwd{barplot}. The special symbol \texttt{<-} performs an \emph{assignment}, taking the output of one line of code and saving it into an object in your workspace.   This is another important idea that we'll return to later.

\begin{exercise}
Create a numerical summary for \hlstd{height} and \hlstd{age}, and compute the interquartile range for each. Compute the relative frequency distribution for \hlstd{gender} and \hlstd{exerany}. How many males are in the sample? What proportion of the sample reports being in excellent health?
\end{exercise}

The \hlkwd{table} command can be used to tabulate any number of variables that you provide.  For example, to examine which participants have smoked across each gender, we could use the following.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{table}\hlstd{(cdc}\hlopt{$}\hlstd{gender,cdc}\hlopt{$}\hlstd{smoke100)}
\end{alltt}
\end{kframe}
\end{knitrout}

Here, we see column labels of 0 and 1. Recall that 1 indicates a respondent has smoked at least 100 cigarettes. The rows refer to gender. To create a mosaic plot of this table, we would enter the following command.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{mosaicplot}\hlstd{(}\hlkwd{table}\hlstd{(cdc}\hlopt{$}\hlstd{gender,cdc}\hlopt{$}\hlstd{smoke100))}
\end{alltt}
\end{kframe}
\end{knitrout}

We could have accomplished this in two steps by saving the table in one line and applying \hlkwd{mosaicplot} in the next (see the table/barplot example above).

\begin{exercise}
What does the mosaic plot reveal about smoking habits and gender?
\end{exercise}

\subsection*{Interlude: How R thinks about data}
We mentioned that R stores data in data frames, which you might think of as a type of spreadsheet. Each row is a different observation (a different respondent) and each column is a different variable (the first is \hlstd{genhlth}, the second \hlstd{exerany} and so on). We can see the size of the data frame next to the object name in the workspace or we can type

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{dim}\hlstd{(cdc)}
\end{alltt}
\end{kframe}
\end{knitrout}

which will return the number of rows and columns. Now, if we want to access a subset of the full data frame, we can use row-and-column notation. For example, to see the sixth variable of the 567$^{\textrm{th}}$ respondent, use the format

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc[}\hlnum{567}\hlstd{,}\hlnum{6}\hlstd{]}
\end{alltt}
\end{kframe}
\end{knitrout}

which means we want the element of our data set that is in the 567$^{\textrm{th}}$ row (meaning the 567$^{\textrm{th}}$ person or observation) and the 6$^{\textrm{th}}$ column (in this case, weight). We know that \hlstd{weight} is the 6$^{\textrm{th}}$ variable because it is the 6$^{\textrm{th}}$ entry in the list of variable names

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{names}\hlstd{(cdc)}
\end{alltt}
\end{kframe}
\end{knitrout}

To see the weights for the first 10 respondents we can type

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc[}\hlnum{1}\hlopt{:}\hlnum{10}\hlstd{,}\hlnum{6}\hlstd{]}
\end{alltt}
\end{kframe}
\end{knitrout}

In this expression, we have asked just for rows in the range 1 through 10.  R uses the ``:'' to create a range of values, so 1:10 expands to 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. You can see this by entering

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlnum{1}\hlopt{:}\hlnum{10}
\end{alltt}
\end{kframe}
\end{knitrout}

Finally, if we want all of the data for the first 10 respondents, type

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc[}\hlnum{1}\hlopt{:}\hlnum{10}\hlstd{,]}
\end{alltt}
\end{kframe}
\end{knitrout}

By leaving out an index or a range (we didn't type anything between the comma and the square bracket), we get all the columns. When starting out in R, this is a bit counterintuitive. As a rule, we omit the column number to see all columns in a data frame. Similarly, if we leave out an index or range for the rows, we would access all the observations, not just the 567$^{\textrm{th}}$, or rows 1 through 10. Try the following to see the weights for all 20,000 respondents fly by on your screen

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc[,}\hlnum{6}\hlstd{]}
\end{alltt}
\end{kframe}
\end{knitrout}

Recall that column 6 represents respondents' weight, so the command above reported all of the weights in the data set. An alternative method to access the weight data is by referring to the name.  Previously, we typed \hlkwd{names}\hlstd{(}\hlstd{cdc}\hlstd{)} to see all the variables contained in the cdc data set. We can use any of the variable names to select items in our data set. 

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc}\hlopt{$}\hlstd{weight}
\end{alltt}
\end{kframe}
\end{knitrout}

The dollar-sign tells R to look in data frame \hlstd{cdc} for the column called \hlstd{weight}.  Since that's a single vector, we can subset it with just a single index inside square brackets.  We see the weight for the 567$^{\textrm{th}}$ respondent by typing

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc}\hlopt{$}\hlstd{weight[}\hlnum{567}\hlstd{]}
\end{alltt}
\end{kframe}
\end{knitrout}

Similarly, for just the first 10 respondents

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc}\hlopt{$}\hlstd{weight[}\hlnum{1}\hlopt{:}\hlnum{10}\hlstd{]}
\end{alltt}
\end{kframe}
\end{knitrout}

The command above returns the same result as the \hlstd{cdc[1:10,6]} command. Both row-and-column notation and dollar-sign notation are widely used, which one you choose to use depends on your personal preference.

\subsection*{A little more on subsetting}
It's often useful to extract all individuals (cases) in a data set that have specific characteristics. We accomplish this through \emph{conditioning} commands.  First, consider expressions like

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc}\hlopt{$}\hlstd{gender} \hlopt{==} \hlstr{"m"}
\end{alltt}
\end{kframe}
\end{knitrout}

or

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{cdc}\hlopt{$}\hlstd{age} \hlopt{>} \hlnum{30}
\end{alltt}
\end{kframe}
\end{knitrout}

These commands produce a series of \hlnum{TRUE} and \hlnum{FALSE}  values. There is one value for each respondent, where \hlnum{TRUE} indicates that the person was male (via the first command) or older than 30 (second command).

Suppose we want to extract just the data for the men in the sample, or just for those over 30. We can use the R function \hlkwd{subset} to do that for us. For example, the command

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{mdata} \hlkwb{<-} \hlkwd{subset}\hlstd{(cdc, cdc}\hlopt{$}\hlstd{gender} \hlopt{==} \hlstr{"m"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

will create a new data set called \hlstd{mdata} that contains only the men from the \hlstd{cdc} data set. In addition to finding it in your workspace alongside its dimensions, you can take a peek at the first several rows as usual

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{head}\hlstd{(mdata)}
\end{alltt}
\end{kframe}
\end{knitrout}

This new data set contains all the same variables but just under half the rows. It is also possible to tell R to keep only specific variables, which is a topic we'll discuss in a future lab. For now, the important thing is that we can carve up the data based on values of one or more variables.

As an aside, you can use several of these conditions together with \texttt{\&} and \texttt{|}. The \texttt{\&} is read ``and'' so that

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{m_and_over30} \hlkwb{<-} \hlkwd{subset}\hlstd{(cdc, cdc}\hlopt{$}\hlstd{gender} \hlopt{==} \hlstr{"m"} \hlopt{&} \hlstd{cdc}\hlopt{$}\hlstd{age} \hlopt{>} \hlnum{30}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

will give you the data for men over the age of 30. The \texttt{|} character is read ``or'' so that

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{m_or_over30} \hlkwb{<-} \hlkwd{subset}\hlstd{(cdc, cdc}\hlopt{$}\hlstd{gender} \hlopt{==} \hlstr{"m"} \hlopt{|} \hlstd{cdc}\hlopt{$}\hlstd{age} \hlopt{>} \hlnum{30}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

will take people who are men or over the age of 30 (why that's an interesting group is hard to say, but right now the mechanics of this are the important thing). In principle, you may use as many ``and'' and ``or'' clauses as you like when forming a subset.

\begin{exercise}
Create a new object called \hlstd{under23\_and\_smoke} that contains all observations of respondents under the age of 23 that have smoked 100 cigarettes in their lifetime. Write the command you used to create the new object as the answer to this exercise.
\end{exercise}

\subsection*{Quantitative data}

With our subsetting tools in hand, we'll now return to the task of the day: making basic summaries of the BRFSS questionnaire.  We've already looked at categorical data such as \hlstd{smoke} and \hlstd{gender} so now let's turn our attention to quantitative data.  Two common ways to visualize quantitative data are with box plots and histograms.  We can construct a box plot for a single variable with the following command.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{boxplot}\hlstd{(cdc}\hlopt{$}\hlstd{height)}
\end{alltt}
\end{kframe}
\end{knitrout}

You can compare the locations of the components of the box by examining the summary statistics.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(cdc}\hlopt{$}\hlstd{height)}
\end{alltt}
\end{kframe}
\end{knitrout}

Confirm that the median and upper and lower quartiles reported in the numerical summary match those in the graph. The purpose of a boxplot is to provide a thumbnail sketch of a variable for the purpose of comparing across several categories. So we can, for example, compare the heights of men and women with 

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{boxplot}\hlstd{(cdc}\hlopt{$}\hlstd{height} \hlopt{\mytilde} \hlstd{cdc}\hlopt{$}\hlstd{gender)}
\end{alltt}
\end{kframe}
\end{knitrout}

The notation here is new. The \texttildelow~character can be read ``versus'' or ``as a function of''. So we're asking R to give us a box plots of heights where the groups are defined by gender. 

Next let's consider a new variable that doesn't show up directly in this data set: Body Mass Index (BMI). BMI is a weight to height ratio and can be calculated as.

\[ BMI = \frac{weight~(lb)}{height~(in)^2} * 703\symbolfootnote[2]{703 is the approximate conversion factor to change units from metric (meters and kilograms) to imperial (inches and pounds)} \]

The following two lines first make a new object called \hlstd{bmi} and then creates box plots of these values, defining groups by the variable \hlstd{cdc\$genhlth}.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{bmi} \hlkwb{<-} \hlstd{(cdc}\hlopt{$}\hlstd{weight} \hlopt{/} \hlstd{cdc}\hlopt{$}\hlstd{height}\hlopt{^}\hlnum{2}\hlstd{)} \hlopt{*} \hlnum{703}

\hlkwd{boxplot}\hlstd{(bmi} \hlopt{\mytilde} \hlstd{cdc}\hlopt{$}\hlstd{genhlth)}
\end{alltt}
\end{kframe}
\end{knitrout}

Notice that the first line above is just some arithmetic, but it's applied to all 20,000 numbers in the \hlstd{cdc} data set. That is, for each of the 20,000 participants, we take their weight, divide by their height-squared and then multiply by 703. The result is 20,000 BMI values, one for each respondent. This is one reason why we like R: it lets us perform computations like this using very simple expressions.

\begin{exercise}
What does this box plot show? Pick another categorical variable from the data set and see how it relates to BMI. List the variable you chose, why you might think it would have a relationship to BMI,  and indicate what the figure seems to suggest. 
\end{exercise}

Finally, let's make some histograms. We can look at the histogram for the age of our respondents with the command

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{hist}\hlstd{(cdc}\hlopt{$}\hlstd{age)}
\end{alltt}
\end{kframe}
\end{knitrout}

Histograms are generally a very good way to see the shape of a single distribution, but that shape can change depending on how the data is split between the different bins.  You can control the number of bins by adding an argument to the command. In the next two lines, we first make a default histogram of \hlstd{bmi} and then one with 50 breaks.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{hist}\hlstd{(bmi)}

\hlkwd{hist}\hlstd{(bmi,} \hlkwc{breaks} \hlstd{=} \hlnum{50}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

Note that you can flip between plots that you've created by clicking the forward and backward arrows in the lower right region of RStudio, just above the plots.  How do these two histograms compare?

At this point, we've done a good first pass at analyzing the information in the BRFSS questionnaire.  We've found an interesting association between smoking and gender, and we can say something about the relationship between people's assessment of their general health and their own BMI.  We've also picked up essential computing tools -- summary statistics, subsetting, and plots -- that will serve us well throughout this course.

\vfill

\subsection*{On Your Own}

\begin{enumerate}
\item Make a scatterplot of weight versus desired weight. Describe the relationship between these two variables.
\item Let's consider a new variable: the difference between desired weight (\hlstd{wtdesire}) and current weight (\hlstd{weight}). Create this new variable by subtracting the two columns in the data frame and assigning them to a new object called \hlstd{wdiff}.
\item What type of data is \hlstd{wdiff}? If an observation \hlstd{wdiff} is 0, what does this mean about the person's weight and desired weight. What if \hlstd{wdiff} is positive or negative?
\item Describe the distribution of \hlstd{wdiff} in terms of its center, shape, and spread, including any plots you use. What does this tell us about how people feel about their current weight?
\item Using numerical summaries and a side-by-side box plot, determine if men tend to view their weight differently than women.
\item Now it's time to get creative. Find the mean and standard deviation of \hlstd{weight} and determine what proportion of the weights are within one standard deviation of the mean.
\item What concepts from the textbook are covered in this lab?  What concepts, if any, are not covered in the textbook?  Have you seen these concepts elsewhere, e.g. lecture, discussion section, previous labs, or homework problems?  Be specific in your answer.
\end{enumerate}

\end{document}
